package com.cvte.boe.media.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

public class DiskUtil {
    private static final String DISK_LEVEL_B_DES = "B";
    private static final int DISK_LEVEL_GB = 0x2;
    private static final String DISK_LEVEL_GB_DES = "GB";
    private static final int DISK_LEVEL_KB = 0x0;
    private static final String DISK_LEVEL_KB_DES = "KB";
    private static final int DISK_LEVEL_MB = 0x1;
    private static final String DISK_LEVEL_MB_DES = "MB";
    private static final int DISK_LEVEL_TB = 0x3;
    private static final String DISK_LEVEL_TB_DES = "TB";
    private static final String DISK_LEVEL_UNKNOWN_DEFAULT_DES = " ";
    private static final String TAG = "DiskUtil";

    public static String getDiskSizeString(long size, int unknownid, int kbid, int mbid, int gbid, int tbid) {
        return getDiskSizeString(null, size, unknownid, kbid, mbid, gbid, tbid);
    }

    public static String getDiskSizeString(Context context, long size, int unknownid, int kbid, int mbid, int gbid, int tbid) {
        if (size <= 0L) {
            String str6;
            if (context == null)
                str6 = "0" + DISK_LEVEL_KB_DES;
            else
                str6 = "" + ResCacheLoadUtil.getStringById(context, unknownid);
            return str6;
        }
        int i = 0;
        long l1 = size >> 10;
        do {
            if (l1 <= 0L)
                break;
            i++;
            l1 >>= 10;
        }
        while (i != 3);
        String str1 = "" + (size >> i * 10);
        int j;
        if (i - 1 >= 0) {
            long l2 = size % (1 << i * 10) >> 10 * (i - 1);
            if (l2 != 0L) {
                String str4 = String.valueOf(l2 / 1024.0D);
                if (str4.length() <= 3)
                    j = 3;
                else
                    j = 4;
                String str5 = str1 + ".";
                str1 = str5 + String.valueOf(str4).substring(2, j);
            }
        }
        String str2 = str1 + " ";
        String str3;
        switch (i) {
            default:
                if (context != null) {
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, unknownid);
                } else {
                    str3 = str2 + DISK_LEVEL_UNKNOWN_DEFAULT_DES;
                }
                break;
            case DISK_LEVEL_KB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_KB_DES;
                } else {
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, kbid);
                }
                break;
            case DISK_LEVEL_MB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_MB_DES;
                } else
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, mbid);
                break;
            case DISK_LEVEL_GB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_GB_DES;
                } else
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, gbid);
                break;
            case DISK_LEVEL_TB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_TB_DES;
                } else
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, tbid);
                break;
        }
        return str3;
    }

    public static String getDiskSizeStringL(Context context, long size, int unknownid, int kbid, int mbid, int gbid, int tbid) {
        if (size <= 0L) {
            String str5;
            if (context == null) {
                str5 = "0" + DISK_LEVEL_KB_DES;
            } else {
                str5 = "" + ResCacheLoadUtil.getStringById(context, unknownid);
            }
            return str5;
        }
        int iLevel = 0;
        long sizeTmp = size >> 10;
        do {
            if (sizeTmp <= 0L)
                break;
            iLevel++;
            sizeTmp >>= 10;
        }
        while (iLevel != 3);
        String str1 = "" + (size >> iLevel * 10);
        if (iLevel - 1 >= 0) {
            long l2 = size % (1 << iLevel * 10) >> 10 * (iLevel - 1);
            if (l2 != 0L) {
                String str4 = str1 + ".";
                str1 = str4 + String.valueOf(l2).charAt(0);
            }
        }
        String str2 = str1 + " ";
        String str3;
        switch (iLevel) {
            default:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_UNKNOWN_DEFAULT_DES;
                } else {
                    str3 = str2 + ResCacheLoadUtil.getStringById(context, unknownid);
                }
                break;
            case DISK_LEVEL_KB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_KB_DES;
                    break;
                }
                str3 = str2 + ResCacheLoadUtil.getStringById(context, kbid);
                break;
            case DISK_LEVEL_MB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_MB_DES;
                    break;
                }
                str3 = str2 + ResCacheLoadUtil.getStringById(context, mbid);
                break;
            case DISK_LEVEL_GB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_GB_DES;
                    break;
                }
                str3 = str2 + ResCacheLoadUtil.getStringById(context, gbid);
                break;
            case DISK_LEVEL_TB:
                if (context == null) {
                    str3 = str2 + DISK_LEVEL_TB_DES;
                    break;
                }
                str3 = str2 + ResCacheLoadUtil.getStringById(context, tbid);
                break;
        }
        return str3;
    }

    public static String getDiskName(String mountPath) {
        if (mountPath == null) {
            return "";
        }
        int index = mountPath.lastIndexOf(File.separator);
        if (index != -0x1) {
            return mountPath.substring((index + 0x1));
        }
        return "";
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static float getDataFreeSizeInMB() {
        File data = Environment.getDataDirectory();
        StatFs dataStatus = new StatFs(data.getPath());
        long availBlocks = dataStatus.getAvailableBlocksLong();
        long blockSize = dataStatus.getBlockSizeLong();
        float freeSpace = (float) (((availBlocks * blockSize) / 1024L) / 1024L);
        LogUtil.d("DiskUtil", "free sapce for cache " + freeSpace);
        return freeSpace;
    }
    public static String getFileSizeStringL(long size) {
        long sizeTmp1 = size >> 10;
        if(sizeTmp1 <= 0L){
            return size + " " +DISK_LEVEL_B_DES;
        }else{
            size = sizeTmp1;
        }
        int iLevel = 0;
        long sizeTmp = size >> 10;
        do {
            if (sizeTmp <= 0L)
                break;
            iLevel++;
            sizeTmp >>= 10;
        }
        while (iLevel != 3);
        String str1 = "" + (size >> iLevel * 10);
        if (iLevel - 1 >= 0) {
            long l2 = size % (1 << iLevel * 10) >> 10 * (iLevel - 1);
            if (l2 != 0L) {
                String str4 = str1 + ".";
                str1 = str4 + String.valueOf(l2).charAt(0);
            }
        }
        String str2 = str1 + " ";
        String str3;
        switch (iLevel) {
            default:
                str3 = str2 + DISK_LEVEL_UNKNOWN_DEFAULT_DES;
                break;
            case DISK_LEVEL_KB:
                str3 = str2 + DISK_LEVEL_KB_DES;
                break;
            case DISK_LEVEL_MB:
                str3 = str2 + DISK_LEVEL_MB_DES;
                break;
            case DISK_LEVEL_GB:
                str3 = str2 + DISK_LEVEL_GB_DES;
                break;

            case DISK_LEVEL_TB:
                str3 = str2 + DISK_LEVEL_TB_DES;
                break;

        }
        return str3;
    }
}
